<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="标题">
                <a-input v-model="queryParam.title" placeholder="请输入标题"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                  <a-select-option :value="0">未读</a-select-option>
                  <a-select-option :value="1">接受</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
      
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons"">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="primary" slot-scope="text, record, index">
          {{ record.id}}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>

		<span slot="endtime" slot-scope="text">
		  {{ text | dateTimeFilter2}}
		</span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
		<span slot="time" slot-scope="text">
		  {{ text | dateTimeFilter}}
		</span>
		
		<span slot="actions" slot-scope="text, record">
		  <template>
			<a-button type="danger" @click="handleOffer(record)" v-if="record.status == 0">接受Offer</a-button>
		  </template>
		</span>
      </s-table>
	  
	  <a-modal v-if="visible" v-model="visible" title="接受Offer" @ok="handleOfferOk">
	  		<a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
	  		    <a-form-item label="工资">
	               <a-input type="number" v-model="mdl.salary" disabled/>
	  		    </a-form-item>
				<a-form-item label="offer内容">
				  <a-textarea
				  v-model="mdl.info"d
				  :auto-size="{ minRows: 6, maxRows: 10 }"
				  disabled
				  ></a-textarea>
				</a-form-item>
				<a-form-item label="是否接受" required>
				   <a-select v-decorator="['status', { rules: [{ required: true, message: '请选择' }] }]">
				          <a-select-option :value="0">忽略</a-select-option>
				   		  <a-select-option :value="1">接受</a-select-option>
				   </a-select>
				</a-form-item>
	  		</a-form>
	  </a-modal>
	  
	  
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { page,edit } from '@/api/offer'
import { LOGIN_ID } from '@/store/mutation-types'
import storage from 'store'


const columns = [
  {
    title: 'ID',
    scopedSlots: { customRender: 'primary' }
  },
  {
    title: '标题',
    dataIndex: 'apply.recruit.title'
  },
  {
    title: '姓名',
    dataIndex: 'apply.resume.creater.userInfo.name'
  },
  {
    title: '简历',
    dataIndex: 'apply.resume.name'
  },
  {
    title: '岗位',
    dataIndex: 'apply.recruit.job'
  },
  {
    title: '工资',
    dataIndex: 'salary'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
	scopedSlots: { customRender: 'time' }
  },
  {
    title: '操作',
	width: '200px',
  	scopedSlots: { customRender: 'actions' }
  },
]

const statusMap = {
  0: {
    status: 'processing',
    text: '未读'
  },
  // 1: {
  //   status: 'processing',
  //   text: '运行中'
  // },
  // 2: {
  //   status: 'success',
  //   text: '已上线'
  // },
  1: {
    status: 'success',
    text: '接受'
  }
}

export default {
  name: 'Offer',
  components: {
    STable
  },
  data () {
    this.columns = columns
    return {
	  form: this.$form.createForm(this),
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
		this.queryParam.applyer = storage.get(LOGIN_ID) //默认查询自己的offer
        const requestParameters = Object.assign({}, parameter, {parameter:this.queryParam})
        console.log('loadData request parameters:', requestParameters)
        return page(requestParameters)
          .then(res => {
            return res.data
          })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    },
	dateTimeFilter(time){
		return moment(time).format('YYYY-MM-DD HH:mm:ss')
	},
	dateTimeFilter2(time){
		return moment(time).format('YYYY-MM-DD')
	},
	docFilter(attach){
		return "http://localhost:8080/static/" + attach
	}
  },
  methods: {
	handleOffer(record){
		this.visible = true
		this.mdl = { ...record }
	},
	handleOfferOk(){
		this.confirmLoading = true
		this.form.validateFields((errors, values) => {
			if (!errors) {
				const data = {
					 id: this.mdl.id,
					 status: values.status,
					 salary: this.mdl.salary,
					 
				}
				edit(data).then(res => {
					  if(res.code == 200){
						  // 重置表单数据
						  this.form.resetFields()
						  // 刷新表格
						  this.$refs.table.refresh(true)
						  this.$message.info('获取offer成功')
						  this.visible = false
					  }else{
						  this.$message.error('获取offer失败')
					  }
				}).finally(()=>{
					  this.confirmLoading = false
				})
			}
		 })
	}
  }
}
</script>
